<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" type="info" icon="el-icon-refresh" @click="handleFilter">
        Refresh
      </el-button>
      <el-button v-if="addBtnRights" class="filter-item" type="primary" icon="el-icon-plus" @click="handleAdd">
        Add
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column label="Avatar" width="70">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" width="40" height="40" class="user-avatar"><img>
        </template>
      </el-table-column>
      <el-table-column label="Account">
        <template slot-scope="scope">
          {{ scope.row.account }}
        </template>
      </el-table-column>
      <el-table-column label="Create At" width="200" align="center">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.create_at }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Update At" width="200" align="center">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          {{ scope.row.update_at }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="Status" width="110" align="center">
        <template slot-scope="scope">
          <el-switch v-if="editBtnRights" :value="scope.row.status===1?true:false" active-color="#13ce66" inactive-color="#ff4949" @change="changeStatus($event, scope.row)" />
          <el-switch v-else :value="scope.row.status===1?true:false" active-color="#13ce66" inactive-color="#ff4949" disabled="disabled" @change="changeStatus($event, scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="Introduction" align="left">
        <template slot-scope="scope">
          {{ scope.row.introduction }}
        </template>
      </el-table-column>
      <el-table-column label="Operation" align="center">
        <template slot-scope="{row}">
          <el-button v-if="addBtnRights" type="primary" icon="el-icon-edit" circle @click="handleEdit(row)" />
          <el-button v-if="delBtnRights" type="danger" icon="el-icon-delete" circle @click="handleDel(row)" />
          <el-button v-if="resetPwdBtnRights" type="warning" icon="el-icon-service" circle @click="handleResetPwd(row)" />
        </template>
      </el-table-column>
    </el-table>
    <!--pagination-->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getUserList" />
    <!--edit-form-->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :model="temp" label-position="left" label-width="100px" style="width: 450px; margin-left:50px;">
        <el-form-item label="Account" prop="account">
          <el-input v-model="temp.account" :disabled="dialogStatus==='update'?true:false" />
        </el-form-item>
        <el-form-item label="Password" prop="password">
          <el-input ref="password" v-model="temp.password" :type="passwordType" />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-form-item label="Introduction" prop="introduction">
          <el-input v-model="temp.introduction" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          Cancel
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          Confirm
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { getUserList, delUser, updateUser, createUser, updateStatus } from '@/api/user'
import { checkBtnAuth } from '@/utils/auth'
import Pagination from '@/components/Pagination'
export default {
  name: 'UserList',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        status: 'success'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      temp: {
        user_id: undefined,
        introduction: 1,
        account: '',
        password: '',
        status: 1
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      dialogPvVisible: false,
      pvData: [],
      downloadLoading: false,
      passwordType: 'password',
      resetPwdBtnRights: false,
      addBtnRights: false,
      editBtnRights: false,
      delBtnRights: false
    }
  },
  created() {
    this.getUserList()
  },
  mounted() {
    this.addBtnRights = checkBtnAuth('/user/create')
    this.delBtnRights = checkBtnAuth('/user/del')
    this.editBtnRights = checkBtnAuth('/user/update')
    this.resetPwdBtnRights = checkBtnAuth('/user/resetPwd')
  },
  methods: {
    resetTemp() {
      this.temp = {
        user_id: undefined,
        introduction: '',
        account: '',
        password: '',
        status: 1
      }
    },
    changeStatus(newValue, row) {
      row.status = newValue ? 1 : 0
      updateStatus(row).then(response => {})
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      // 异步更新
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleAdd() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDel(row) {
      // Delete User
      delUser(row.user_id).then(response => {
        if (response.code === 100000) {
          this.$notify({
            title: 'Success',
            message: 'Delete Successfully',
            type: 'success',
            duration: 2000
          })
          // this.handleFilter()
        } else {
          this.$message({
            message: 'Failure',
            type: 'error'
          })
        }
      })
    },
    // 编辑
    handleEdit(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      // console.log('edit:' + row.user_id)
    },
    updateData() {
      updateUser(this.temp).then(response => {
        if (response.code === 100000) {
          this.$notify({
            title: 'Success',
            message: 'Update Successfully',
            type: 'success',
            duration: 2000
          })
          this.dialogStatus = ''
          this.dialogFormVisible = false
          // this.handleFilter()
        } else {
          this.$message({
            message: 'Failure',
            type: 'error'
          })
        }
      })
    },
    createData() {
      // Create User
      createUser(this.temp).then(response => {
        if (response.code === 100000) {
          this.$notify({
            title: 'Success',
            message: 'Delete Successfully',
            type: 'success',
            duration: 2000
          })
          // this.handleFilter()
          this.dialogFormVisible = false
        } else {
          this.$message({
            message: 'Failure',
            type: 'error'
          })
        }
      })
    },
    handleResetPwd(row) {
      // confirm
      console.log('reset:' + row.user_id)
    },
    getUserList() {
      this.listLoading = true
      getUserList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.count
        this.listLoading = false
      })
    },
    handleFilter() {
      this.getUserList()
    }
  }
}
</script>

<style scoped>
.filter-container {
  margin-bottom: 15px
}
.show-pwd {
  position: absolute;
  right: 10px;
  top: 3px;
  font-size: 16px;
  cursor: pointer;
  user-select: none;
}
</style>

